﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;


namespace Process.BLL
{
    /// <summary>
    /// Using memory to create Cart
    /// </summary>
    class TempCart
    {
        public string ProductID;
        public int Quantity;
        public string ProductName;
        public decimal UnitPrice;

        /// <summary>
        /// Init Cart
        /// </summary>
        /// <param name="productID">ProductId</param>
        /// <param name="quantity">Quantity</param>
        /// <param name="productName">ProductName</param>
        /// <param name="unitPrice">UnitPrice</param>
        public TempCart(string productID, int quantity,string productName,decimal unitPrice)
        {
            ProductID = productID;
            Quantity = quantity;
            ProductName = productName;
            UnitPrice = unitPrice;
        }
    }
    public class CartBLL
    {
        private ArrayList lstProduct = new ArrayList();
        private decimal totalAmount;        
        /// <summary>
        /// Create Cart(combine with session["UserLogin"]?
        /// </summary>
        /// <param name="productID"></param>
        /// <param name="quantity"></param>
        /// <param name="productName"></param>
        /// <param name="unitPrice"></param>

        public void CreateTempCart(string productID, int quantity,string productName,decimal unitPrice)
        {
            TempCart temp = null;
            Boolean isExist = false;
            for (int i = 0; i < lstProduct.Count; i++)
            {
                temp = (TempCart)lstProduct[i];
                if (temp.ProductID.Contains(productID))
                {
                    isExist = true;
                    break;
                }
            }

            if (isExist)
            {
                temp.Quantity += quantity;
            }

            else
            {
                temp = new TempCart(productID, quantity, productName,unitPrice);
                lstProduct.Add(temp);
            }
        }

    /// <summary>
    /// Create Table Cart
    /// </summary>
    /// <returns>Datatabsle</returns>
        public DataTable CreateCart()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("productID", System.Type.GetType("System.String"));
            dt.Columns.Add("ProductName", System.Type.GetType("System.String"));
            dt.Columns.Add("Quantity", System.Type.GetType("System.String"));
            dt.Columns.Add("UnitPrice", System.Type.GetType("System.String"));
            dt.Columns.Add("LineNet", System.Type.GetType("System.String"));

            TempCart temp = null;
            DataRow Rowtemp;

            for (int i = 0; i < lstProduct.Count; i++)
            {
                temp = (TempCart)lstProduct[i];
                Rowtemp = dt.NewRow();
                Rowtemp["productID"] = temp.ProductID;
                Rowtemp["productName"] = temp.ProductName;
                Rowtemp["Quantity"] = temp.Quantity;
                Rowtemp["UnitPrice"] = temp.UnitPrice;
           
                dt.Rows.Add(Rowtemp);

            }
            return dt;

        }

       /// <summary>
       /// return total product in Cart
       /// </summary>
       /// <returns></returns>
        public int TotalProduct()
        {
            return lstProduct.Count;
        }

        public decimal GetTotalAmount()
        {
            totalAmount = 0;
            TempCart cartItem;
            try
            {
                for (int i = 0; i < lstProduct.Count; i++)
                {
                    cartItem = (TempCart)lstProduct[i];
                    totalAmount += cartItem.Quantity * cartItem.UnitPrice ;
                }
            }
            catch
            {
                throw new Exception { };
            }
            finally {
              
            }
            return totalAmount;
                
            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public string GetProductIDByIndex(int index)
        {
            string ProductID = "";
            try
            {
                TempCart temp = (TempCart)lstProduct[index];
                ProductID = temp.ProductID;
            }
            catch
            {
                throw new Exception { };
            }
            finally
            {

            }
            return ProductID;
        }

        /// <summary>
        /// Get quantity of product in cart
        /// </summary>
        /// <param name="index"></param>
        /// <returns>total product in cart</returns>
        public int GetQuantity(int index)
        {
            int quantity = 0;
            try
            {
                TempCart item = (TempCart)lstProduct[index];
                quantity = item.Quantity;
            }
            catch
            {
                throw new Exception { };
            }
            finally
            {

            }
            return quantity;
        }
      

        /// <summary>
        /// Remove All Product
        /// </summary>
        public void RemoveAllProduct()
        {
            lstProduct.Clear();
        }

       /// <summary>
       /// Update Information in Cart(quantity,totalAmout)
       /// </summary>
       /// <param name="productID"></param>
       /// <param name="total"></param>
        public void UpdateProductInCart(string productID, int total)
        {
            TempCart temp;
            for (int i = 0; i < lstProduct.Count; i++)
            {
                temp = (TempCart)lstProduct[i];
                if (temp.ProductID.Contains(productID))
                {
                    temp.Quantity = total;
                    break;
                }

            }
        }

        /// <summary>
        /// Remove product
        /// </summary>
        /// <param name="productID">productID</param>
        public void RemoveProductByID(string productID)
        {
            TempCart temp;
            for (int i = 0; i < lstProduct.Count; i++)
            {
                temp = (TempCart)lstProduct[i];
                //unit ProductID
                if (temp.ProductID.Contains(productID))
                {
                    lstProduct.RemoveAt(i);
                }
            }
        }
    }
}
